package android.alliance.ads;

import android.alliance.ads.actions.OnClickAction;
import android.alliance.ads.enumerations.FullscreenTarget;
import android.alliance.ads.helper.AllianceBitmapScaler;
import android.alliance.ads.helper.BitmapScaler;
import android.alliance.ads.helper.ScreenSizeHelper;
import android.alliance.ads.interfaces.IBannerCallback;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;


/**
 * Android Alliance - Ads-Library
 *
 * Copyright (c) 2011-2014 Android Alliance
 *
 * http://www.android-alliance.org
 * http://www.droidspirit.com
 * http://www.strangeoptics.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
 * this file except in compliance with the License. You may obtain a copy of the License at  
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 * 
 *  E-Mail: support@droidspirit.com
 */
public class FullscreenDialogActivity extends Activity {
	
	private FullscreenBanner fsb = FullscreenBanner.getInstance();
	private Activity parentActivity;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom_dialog);

		parentActivity = this;
		
		int displayWidth = 0;
		int displayHeight = 0;
		
		// Close-Button
		Button btClose = (Button) findViewById(R.id.btCloseAds);
		btClose.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				parentActivity.finish();
			}
		});

		
		int currentOrientation = getResources().getConfiguration().orientation;
		
		if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {

			displayWidth = ScreenSizeHelper.getInstance().getDisplayHeight(this);
			displayHeight = ScreenSizeHelper.getInstance().getDisplayWidth(this);
			
		} else if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
			
			displayWidth = ScreenSizeHelper.getInstance().getDisplayWidth(this);
			displayHeight = ScreenSizeHelper.getInstance().getDisplayHeight(this);
		}
		
		Bitmap banner = new AllianceBitmapScaler().getScaledBitmap(this, fsb.getBannerResource(), null, null, null, true);
		// Set Image to View
		ImageView ivOwnBanner = (ImageView) findViewById(R.id.imgBanner);
		ivOwnBanner.setImageBitmap(banner);
		
		// Set up layout click listener
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.customLayout);
		layout.setLayoutParams(new FrameLayout.LayoutParams(displayWidth, displayHeight));
		layout.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent launchIntent = AdAdapter.getInstance().runOnClickAction(fsb.getTarget());
				
				if(launchIntent != null){
					startActivity(launchIntent);
				}
				
				finish();
			}
		});

	}

	@Override
	public void onBackPressed() {
		finish();
	}

	@Override
	protected void onStop() {
		super.onStop();
		
		IBannerCallback callback = fsb.getBannerCallback();
		
		if(callback != null){
			callback.bannerClicked(FullscreenTarget.FROM_OWN_BANNER);
		}
	}
	
}